package com.gitee.fsmxm.wt.servlet;

import com.gitee.fsmxm.wt.data.Users;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf8");
        String nickname = req.getParameter("nickname");
        if (nickname == null || "".equals(nickname.trim())) {
            resp.sendRedirect(req.getServletContext().getContextPath());
            return;
        }

        if (Users.isNicknameLoggedIn(nickname)) {
            resp.sendRedirect(req.getServletContext().getContextPath()
                    + "?error_msg=" + URLEncoder.encode("该昵称已经被使用，请重新登录", "utf8"));
            return;
        }

        resp.sendRedirect(req.getServletContext().getContextPath()
                + "/chat.html"
                + "?&nickname=" + URLEncoder.encode(nickname.trim(), "utf8"));

    }
}
